package sm4

import (
	"errors"
	"fmt"
	"github.com/tjfoc/gmsm/sm4"
)

type sm4Coder struct {
}

func NewSm4() Sm4Coder {
	return &sm4Coder{}
}
func (s *sm4Coder) Encode(data, key []byte) ([]byte, error) {
	//key := []byte("1234567890abcdef")
	if len(data) == 0 {
		return nil, fmt.Errorf("badlen")
	}
	ecbMsg, err := sm4.Sm4Ecb(key, data, true) //sm4Ecb模式pksc7填充加密
	if err != nil {
		fmt.Println(err)
		return nil, err
	}
	return ecbMsg, nil
}

func (s *sm4Coder) Decode(data, key []byte) ([]byte, error) {
	ecbDec, err := sm4.Sm4Ecb(key, data, false) //sm4Ecb模式pksc7填充解密
	if err != nil {
		fmt.Println(err)
		return nil, err
	}
	if ecbDec == nil {
		return nil, errors.New("sm4 decode error, may be input data error")
	}
	return ecbDec, nil
}
